

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>describe.descriptors.ewaldmatrix &mdash; describe 0.1.0 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/style.css" type="text/css" />
    <link rel="author" title="About these documents" href="../../../about.html" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> describe
          

          
          </a>

          
            
            
              <div class="version">
                0.1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials/tutorials.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../doc/modules.html">Documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../about.html">About</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">describe</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>describe.descriptors.ewaldmatrix</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for describe.descriptors.ewaldmatrix</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span><span class="p">,</span> <span class="n">division</span><span class="p">,</span> <span class="n">print_function</span><span class="p">,</span> <span class="n">unicode_literals</span>
<span class="kn">from</span> <span class="nn">builtins</span> <span class="k">import</span> <span class="p">(</span><span class="nb">bytes</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="nb">open</span><span class="p">,</span> <span class="nb">super</span><span class="p">,</span> <span class="nb">range</span><span class="p">,</span>
                      <span class="nb">zip</span><span class="p">,</span> <span class="nb">round</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="nb">pow</span><span class="p">,</span> <span class="nb">object</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">scipy.special</span> <span class="k">import</span> <span class="n">erfc</span>
<span class="kn">from</span> <span class="nn">describe.descriptors.matrixdescriptor</span> <span class="k">import</span> <span class="n">MatrixDescriptor</span>
<span class="kn">from</span> <span class="nn">describe.core.lattice</span> <span class="k">import</span> <span class="n">Lattice</span>
<span class="kn">from</span> <span class="nn">describe</span> <span class="k">import</span> <span class="n">System</span>
<span class="kn">from</span> <span class="nn">ase</span> <span class="k">import</span> <span class="n">Atoms</span>


<div class="viewcode-block" id="EwaldMatrix"><a class="viewcode-back" href="../../../doc/describe.descriptors.html#describe.descriptors.ewaldmatrix.EwaldMatrix">[docs]</a><span class="k">class</span> <span class="nc">EwaldMatrix</span><span class="p">(</span><span class="n">MatrixDescriptor</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculates an &#39;Ewald matrix&#39; for the a given system.</span>

<span class="sd">    Each entry x_ij of the Ewald matrix will contain the full Coulomb energy</span>
<span class="sd">    for a subsystem consisting of the atoms i and j in the unit cell (or just i</span>
<span class="sd">    on the diagonal) after a constant background charge has been added to</span>
<span class="sd">    counteract any possible net charge in that particular subsystem.</span>

<span class="sd">    The final matrix elements will not be dependent on the value of the</span>
<span class="sd">    screening parameter a that is used.</span>

<span class="sd">    The regular Ewald summation energy cannot be properly divided into parts</span>
<span class="sd">    for each ij pair of atoms in the unit cell, because the terms of the</span>
<span class="sd">    reciprocal and real space components depend on the value of the screening</span>
<span class="sd">    parameter a that is used. This dependency is countered with the scalar</span>
<span class="sd">    self-terms and the possible charge term, which make the sum a constant, but</span>
<span class="sd">    not the .</span>

<span class="sd">    For reference, see:</span>
<span class="sd">        &quot;Crystal Structure Representations for Machine Learning Models of</span>
<span class="sd">        Formation Energies&quot;, Felix Faber, Alexander Lindmaa, Anatole von</span>
<span class="sd">        Lilienfeld, and Rickard Armiento, International Journal of Quantum</span>
<span class="sd">        Chemistry, (2015),</span>
<span class="sd">        https://doi.org/10.1002/qua.24917</span>
<span class="sd">    and</span>
<span class="sd">        &quot;Ewald summation techniques in perspective: a survey&quot;, Abdulnour Y.</span>
<span class="sd">        Toukmaji, John A. Board Jr., Computer Physics Communications, (1996)</span>
<span class="sd">        https://doi.org/10.1016/0010-4655(96)00016-1</span>
<span class="sd">    &quot;&quot;&quot;</span>
<div class="viewcode-block" id="EwaldMatrix.create"><a class="viewcode-back" href="../../../doc/describe.descriptors.html#describe.descriptors.ewaldmatrix.EwaldMatrix.create">[docs]</a>    <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">rcut</span><span class="p">,</span> <span class="n">gcut</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            system (System): Input system.</span>
<span class="sd">            rcut (float): Real space cutoff radius dictating how</span>
<span class="sd">                many terms are used in the real space sum.</span>
<span class="sd">            gcut (float): Reciprocal space cutoff radius.</span>
<span class="sd">            a (float): The screening parameter that controls the width of the</span>
<span class="sd">                Gaussians. If not specified the default value of</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Ensure that we get a System</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">system</span><span class="p">,</span> <span class="n">Atoms</span><span class="p">):</span>
            <span class="n">system</span> <span class="o">=</span> <span class="n">System</span><span class="o">.</span><span class="n">from_atoms</span><span class="p">(</span><span class="n">system</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">get_atomic_numbers</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">q_squared</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">**</span><span class="mi">2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n_atoms</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">system</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">volume</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">get_volume</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sqrt_pi</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>

        <span class="c1"># If a is not specified, we provide the default that is mentioned in</span>
        <span class="c1"># https://doi.org/10.1002/qua.24917. Notice that in that article there</span>
        <span class="c1"># is a mistake as the volume should be squared.</span>
        <span class="k">if</span> <span class="n">a</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">a</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sqrt_pi</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">power</span><span class="p">(</span><span class="mf">0.01</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">n_atoms</span><span class="o">/</span><span class="bp">self</span><span class="o">.</span><span class="n">volume</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">6</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="n">a</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">a_squared</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gcut</span> <span class="o">=</span> <span class="n">gcut</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rcut</span> <span class="o">=</span> <span class="n">rcut</span>

        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">system</span><span class="p">)</span></div>

<div class="viewcode-block" id="EwaldMatrix.get_matrix"><a class="viewcode-back" href="../../../doc/describe.descriptors.html#describe.descriptors.ewaldmatrix.EwaldMatrix.get_matrix">[docs]</a>    <span class="k">def</span> <span class="nf">get_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The total energy matrix. Each matrix element (i, j) corresponds to the</span>
<span class="sd">        total interaction energy in a system with atoms i and j.</span>

<span class="sd">        Args:</span>
<span class="sd">            system(:class:`.System`): The system for which the Ewald matrix is</span>
<span class="sd">                calculated.</span>

<span class="sd">        Returns:</span>
<span class="sd">            np.ndarray: Ewald matrix as 2D array.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Calculate the regular real and reciprocal space sums of the Ewald sum.</span>
        <span class="n">ereal</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calc_real</span><span class="p">(</span><span class="n">system</span><span class="p">)</span>
        <span class="n">erecip</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calc_recip</span><span class="p">(</span><span class="n">system</span><span class="p">)</span>
        <span class="n">total</span> <span class="o">=</span> <span class="n">erecip</span> <span class="o">+</span> <span class="n">ereal</span>

        <span class="c1"># Calculate the modification that makes each entry of the matrix to be</span>
        <span class="c1"># the full Ewald sum of the ij subsystem.</span>
        <span class="n">total</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_calc_subsystem_energies</span><span class="p">(</span><span class="n">total</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">total</span></div>

    <span class="k">def</span> <span class="nf">_calc_self_term</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Calculate the self-term (constant term) of the Ewald sum.</span>

<span class="sd">        This term arises from the interaction between a point charge and the</span>
<span class="sd">        gaussian charge density that is centered on it.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">values</span> <span class="o">=</span> <span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">a</span><span class="o">/</span><span class="bp">self</span><span class="o">.</span><span class="n">sqrt_pi</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">q_squared</span>
        <span class="n">eself</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">eself</span>

    <span class="k">def</span> <span class="nf">_calc_charge_correction</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Calculate the charge correction.</span>

<span class="sd">        Essentially through this correction we add a constant background charge</span>
<span class="sd">        to make the material charge neutral. Any material whose unit cell is</span>
<span class="sd">        not neutral will have infinite energy/volume (the G=0 term in the</span>
<span class="sd">        reciprocal term will be infinite), so we have to make this correction</span>
<span class="sd">        to make the system physical.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">charge_correction</span> <span class="o">=</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">volume</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">a_squared</span><span class="p">)</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
        <span class="k">return</span> <span class="n">charge_correction</span>

    <span class="k">def</span> <span class="nf">_calc_real</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Used to calculate the Ewald real-space sum.</span>

<span class="sd">        Corresponds to equation (5) in</span>
<span class="sd">        https://doi.org/10.1016/0010-4655(96)00016-1</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fcoords</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">get_scaled_positions</span><span class="p">()</span>
        <span class="n">coords</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">get_positions</span><span class="p">()</span>
        <span class="n">n_atoms</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">system</span><span class="p">)</span>
        <span class="n">ereal</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n_atoms</span><span class="p">,</span> <span class="n">n_atoms</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float</span><span class="p">)</span>
        <span class="n">lattice</span> <span class="o">=</span> <span class="n">Lattice</span><span class="p">(</span><span class="n">system</span><span class="o">.</span><span class="n">get_cell</span><span class="p">())</span>

        <span class="c1"># For each atom in the original cell, get the neighbours in the</span>
        <span class="c1"># infinite system within the real space cutoff and calculate the real</span>
        <span class="c1"># space portion of the Ewald sum.</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_atoms</span><span class="p">):</span>

            <span class="c1"># Get points that are within the real space cutoff</span>
            <span class="n">nfcoords</span><span class="p">,</span> <span class="n">rij</span><span class="p">,</span> <span class="n">js</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">get_points_in_sphere</span><span class="p">(</span>
                <span class="n">fcoords</span><span class="p">,</span>
                <span class="n">coords</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">rcut</span><span class="p">,</span>
                <span class="n">zip_results</span><span class="o">=</span><span class="kc">False</span>
            <span class="p">)</span>
            <span class="c1"># Remove the rii term, because a charge does not interact with</span>
            <span class="c1"># itself (but does interact with copies of itself).</span>
            <span class="n">mask</span> <span class="o">=</span> <span class="n">rij</span> <span class="o">&gt;</span> <span class="mf">1e-8</span>
            <span class="n">js</span> <span class="o">=</span> <span class="n">js</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span>
            <span class="n">rij</span> <span class="o">=</span> <span class="n">rij</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span>
            <span class="n">nfcoords</span> <span class="o">=</span> <span class="n">nfcoords</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span>

            <span class="n">qi</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="n">qj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">[</span><span class="n">js</span><span class="p">]</span>

            <span class="n">erfcval</span> <span class="o">=</span> <span class="n">erfc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">*</span> <span class="n">rij</span><span class="p">)</span>
            <span class="n">new_ereals</span> <span class="o">=</span> <span class="n">erfcval</span> <span class="o">*</span> <span class="n">qi</span> <span class="o">*</span> <span class="n">qj</span> <span class="o">/</span> <span class="n">rij</span>

            <span class="c1"># Insert new_ereals</span>
            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_atoms</span><span class="p">):</span>
                <span class="n">ereal</span><span class="p">[</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">new_ereals</span><span class="p">[</span><span class="n">js</span> <span class="o">==</span> <span class="n">k</span><span class="p">])</span>

        <span class="n">ereal</span> <span class="o">*=</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span>
        <span class="k">return</span> <span class="n">ereal</span>

    <span class="k">def</span> <span class="nf">_calc_recip</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Perform the reciprocal space summation. Uses the fastest non mesh-based</span>
<span class="sd">        method described as given by equation (16) in</span>
<span class="sd">        https://doi.org/10.1016/0010-4655(96)00016-1</span>

<span class="sd">        The term G=0 is neglected, even if the system has nonzero charge.</span>
<span class="sd">        Physically this would mean that we are adding a constant background</span>
<span class="sd">        charge to make the cell charge neutral.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">n_atoms</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">n_atoms</span>
        <span class="n">erecip</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n_atoms</span><span class="p">,</span> <span class="n">n_atoms</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float</span><span class="p">)</span>
        <span class="n">coords</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">get_positions</span><span class="p">()</span>

        <span class="c1"># Get the reciprocal lattice points within the reciprocal space cutoff</span>
        <span class="n">rcp_latt</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">*</span><span class="n">system</span><span class="o">.</span><span class="n">get_reciprocal_cell</span><span class="p">()</span>
        <span class="n">rcp_latt</span> <span class="o">=</span> <span class="n">Lattice</span><span class="p">(</span><span class="n">rcp_latt</span><span class="p">)</span>
        <span class="n">recip_nn</span> <span class="o">=</span> <span class="n">rcp_latt</span><span class="o">.</span><span class="n">get_points_in_sphere</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
                                                 <span class="bp">self</span><span class="o">.</span><span class="n">gcut</span><span class="p">)</span>

        <span class="c1"># Ignore the terms with G=0.</span>
        <span class="n">frac_coords</span> <span class="o">=</span> <span class="p">[</span><span class="n">fcoords</span> <span class="k">for</span> <span class="p">(</span><span class="n">fcoords</span><span class="p">,</span> <span class="n">dist</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="ow">in</span> <span class="n">recip_nn</span> <span class="k">if</span> <span class="n">dist</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]</span>

        <span class="n">gs</span> <span class="o">=</span> <span class="n">rcp_latt</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">frac_coords</span><span class="p">)</span>
        <span class="n">g2s</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">gs</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">expvals</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">g2s</span> <span class="o">/</span> <span class="p">(</span><span class="mi">4</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">a_squared</span><span class="p">))</span>
        <span class="n">grs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">gs</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">coords</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:],</span> <span class="mi">2</span><span class="p">)</span>
        <span class="n">factors</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">divide</span><span class="p">(</span><span class="n">expvals</span><span class="p">,</span> <span class="n">g2s</span><span class="p">)</span>
        <span class="n">charges</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span>

        <span class="c1"># Create array where q_2[i,j] is qi * qj</span>
        <span class="n">qiqj</span> <span class="o">=</span> <span class="n">charges</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span> <span class="o">*</span> <span class="n">charges</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">gr</span><span class="p">,</span> <span class="n">factor</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">grs</span><span class="p">,</span> <span class="n">factors</span><span class="p">):</span>

            <span class="c1"># Uses the identity sin(x)+cos(x) = 2**0.5 sin(x + pi/4)</span>
            <span class="n">m</span> <span class="o">=</span> <span class="p">(</span><span class="n">gr</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span> <span class="o">+</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">4</span><span class="p">)</span> <span class="o">-</span> <span class="n">gr</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span>
            <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">m</span><span class="p">)</span>
            <span class="n">m</span> <span class="o">*=</span> <span class="n">factor</span>
            <span class="n">erecip</span> <span class="o">+=</span> <span class="n">m</span>

        <span class="n">erecip</span> <span class="o">*=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">volume</span> <span class="o">*</span> <span class="n">qiqj</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">**</span> <span class="mf">0.5</span>
        <span class="k">return</span> <span class="n">erecip</span>

    <span class="k">def</span> <span class="nf">_calc_subsystem_energies</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ewald_matrix</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Modify the give matrix that consists of the real and reciprocal sums</span>
<span class="sd">        so that each entry x_ij is the full Ewald sum energy of a system</span>
<span class="sd">        consisting of atoms i and j.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">q</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span>

        <span class="c1"># Create the self-term array where q1[i,j] is qi**2 + qj**2, except for</span>
        <span class="c1"># the diagonal, where it is qi**2. The self term corresponds to the</span>
        <span class="c1"># interaction of the point charge with cocentric Gaussian cloud</span>
        <span class="c1"># introduced in the Ewald method.</span>
        <span class="n">q1</span> <span class="o">=</span> <span class="n">q</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">q</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span><span class="o">**</span><span class="mi">2</span>
        <span class="n">diag</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">q1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span>
        <span class="n">np</span><span class="o">.</span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">q1</span><span class="p">,</span> <span class="n">diag</span><span class="p">)</span>
        <span class="n">q1_prefactor</span> <span class="o">=</span> <span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">a</span><span class="o">/</span><span class="bp">self</span><span class="o">.</span><span class="n">sqrt_pi</span>

        <span class="c1"># Create the charge correction array where q2[i,j] is (qi + qj)**2,</span>
        <span class="c1"># except for the diagonal where it is qi**2</span>
        <span class="n">q2</span> <span class="o">=</span> <span class="n">q</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span> <span class="o">+</span> <span class="n">q</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span>
        <span class="n">q2</span> <span class="o">**=</span> <span class="mi">2</span>
        <span class="n">diag</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">q2</span><span class="p">)</span><span class="o">/</span><span class="mi">4</span>
        <span class="n">np</span><span class="o">.</span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">q2</span><span class="p">,</span> <span class="n">diag</span><span class="p">)</span>
        <span class="n">q2_prefactor</span> <span class="o">=</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">volume</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">a_squared</span><span class="p">)</span>
        <span class="n">correction_matrix</span> <span class="o">=</span> <span class="n">q1_prefactor</span><span class="o">*</span><span class="n">q1</span> <span class="o">+</span> <span class="n">q2_prefactor</span><span class="o">*</span><span class="n">q2</span>

        <span class="c1"># Add the terms coming from x_ii and x_jj to the off-diagonal along</span>
        <span class="c1"># with the corrections</span>
        <span class="n">n_atoms</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">n_atoms</span>
        <span class="n">final_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n_atoms</span><span class="p">,</span> <span class="n">n_atoms</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_atoms</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_atoms</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">j</span><span class="p">:</span>
                    <span class="n">final_matrix</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">ewald_matrix</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">pair_term</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">ewald_matrix</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span>
                    <span class="n">self_term_ii</span> <span class="o">=</span> <span class="n">ewald_matrix</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">i</span><span class="p">]</span>
                    <span class="n">self_term_jj</span> <span class="o">=</span> <span class="n">ewald_matrix</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span>
                    <span class="n">energy_total</span> <span class="o">=</span> <span class="n">pair_term</span> <span class="o">+</span> <span class="n">self_term_ii</span> <span class="o">+</span> <span class="n">self_term_jj</span>
                    <span class="n">final_matrix</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">energy_total</span>
        <span class="n">final_matrix</span> <span class="o">+=</span> <span class="n">correction_matrix</span>

        <span class="k">return</span> <span class="n">final_matrix</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright .

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'0.1.0',
            LANGUAGE:'None',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>